package com.example.tangjiangxu.disklrucachedemo;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import libcore.io.DiskLruCache;

public class MainActivity extends Activity {
    String imageUrl = "http://img.my.csdn.net/uploads/201309/01/1378037235_7476.jpg";
    private ImageView mImageview;
    private Button bnDown;
    private Button bnShow;
    private Button bnDelete;
    private Button bnAllDelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bnDown = (Button) findViewById(R.id.bn_down);
        bnDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                downImage();
            }
        });
        bnShow = (Button) findViewById(R.id.bn_show);
        bnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadImage();
                //获得所有缓存文件大小
                float cacheSize = (DiskCacheUtil.getInstance(MainActivity.this).size() * 1.0f / 1024 / 1024);
                bnShow.setText("展示：" + cacheSize + "Mb");
            }
        });
        bnDelete = (Button) findViewById(R.id.bn_delete);
        bnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                remove();
            }
        });
        bnAllDelete = (Button) findViewById(R.id.bn_all_delete);
        bnAllDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    //将所有的缓存数据全部删除， journal文件也会删除
                    DiskCacheUtil.getInstance(MainActivity.this).delete();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        mImageview = (ImageView) findViewById(R.id.img);

    }



    /**
     * 删除图片
     * 一般来说，不需要调用，DiskLruCache会根据open方法时，设置的缓存最大值来自动删除多余的缓存
     */
    private void remove() {
        String key = DiskCacheUtil.getMD5(imageUrl);
        try {
            DiskCacheUtil.getInstance(this).remove(key);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 下载图片
     */
    private void downImage() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String key = DiskCacheUtil.getMD5(imageUrl);
                    DiskLruCache diskLruCache = DiskCacheUtil.getInstance(MainActivity.this);
                    DiskLruCache.Editor editor = diskLruCache.edit(key);
                    if (editor != null) {
                        OutputStream outputStream = editor.newOutputStream(0);
                        if (HttpUtil.downloadUrlToStream(imageUrl, outputStream)) {
                            editor.commit();
                        } else {
                            editor.abort();
                        }
                    }
                    //在onpause中一次
                    //diskLruCache.flush();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 加载图片
     */
    private void loadImage() {
        try {
            String key = DiskCacheUtil.getMD5(imageUrl);
            DiskLruCache.Snapshot snapShot = DiskCacheUtil.getInstance(MainActivity.this).get(key);
            if (snapShot != null) {
                InputStream is = snapShot.getInputStream(0);
                Bitmap bitmap = BitmapFactory.decodeStream(is);
                mImageview.setImageBitmap(bitmap);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        try {
            //flash,用于将内存中的记录同步到日志文件，一般在onPause中调用
            DiskCacheUtil.getInstance(this).flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            //close方法，和open对应的一个方法，关掉以后，
            // 就不能调用DiskLruCache中任何缓存数据的方法，一般在onDestroy中调用
            DiskCacheUtil.getInstance(this).close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
